package com.ncepu.clubmanagement.service.impl;

import com.ncepu.clubmanagement.mapper.MultiMediaMapper;
import com.ncepu.clubmanagement.pojo.*;
import com.ncepu.clubmanagement.pojo.inter.MultiMediaAssist;
import com.ncepu.clubmanagement.service.MultiMediaService;
import com.ncepu.clubmanagement.service.UploadAssist.FileUrlProvider;
import com.ncepu.clubmanagement.service.UploadAssist.FileUrlUpdater;
import com.ncepu.clubmanagement.service.UploadService;
import com.ncepu.clubmanagement.utils.PageDivideUtil;
import com.ncepu.clubmanagement.utils.PageDivideUtilAssist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Stream;

@Service
public class MultiMediaServiceImpl implements MultiMediaService {

    @Autowired
    private MultiMediaMapper multiMediaMapper;

    @Autowired
    private UploadService uploadService;


    @Transactional
    @Override
    public void addMultiMedia(MultiMediaFontOfAdd multiMediaFontOfAdd) throws Exception {
        // 转换为MultiMedia对象
        MultiMedia multiMedia = multiMediaFontOfAdd.toMultiMedia();

        // 调用Mapper层上传
        multiMediaMapper.insertMultiMedia(multiMedia);

        // 执行2个url替换逻辑（将相关文件资源调入到正确的位置中）
        // TODO: 一种可能的未来优化：能否把这里的更新的逻辑组织一个新的方法，进行统一的更新
        if (multiMedia.getCoverUrl() != null) {
            uploadService.universalMoveFile(Integer.toString(multiMedia.getClubId()),
                    "multiMedia", multiMedia.getCoverUrl(),
                    (FileUrlProvider) () -> null,
                    (FileUrlUpdater) url -> multiMediaMapper.updateCoverUrl(url,multiMedia.getMultimediaId()),
                    "cover");
        }
        if (multiMedia.getMultiMediaUrl() != null) {
            uploadService.universalMoveFile(Integer.toString(multiMedia.getClubId()),
                    "multiMedia", multiMedia.getMultiMediaUrl(),
                    (FileUrlProvider) () -> null,
                    (FileUrlUpdater) url -> multiMediaMapper.updateMultiMediaUrl(url,multiMedia.getMultimediaId()),
                    "multiMedia");
        }


    }

    @Override
    @Transactional
    public void updateMultiMedia(MultiMediaChangeableInfo mediaChangeableInfo) throws Exception {

        // 根据可以修改的URL转移URL到正确的位置
        if (mediaChangeableInfo.getCoverUrl() != null) {
            uploadService.universalMoveFile(Integer.toString(mediaChangeableInfo.getMultimediaId()),
                    "multiMedia", mediaChangeableInfo.getCoverUrl(),
                    (FileUrlProvider) () -> null,
                    (FileUrlUpdater) url -> multiMediaMapper.updateCoverUrl(url,mediaChangeableInfo.getMultimediaId()),
                    "cover");
        }
        if (mediaChangeableInfo.getMultiMediaUrl() != null) {
            uploadService.universalMoveFile(Integer.toString(mediaChangeableInfo.getMultimediaId()),
                    "multiMedia", mediaChangeableInfo.getMultiMediaUrl(),
                    (FileUrlProvider) () -> null,
                    (FileUrlUpdater) url -> multiMediaMapper.updateMultiMediaUrl(url,mediaChangeableInfo.getMultimediaId()),
                    "multiMedia");
        }

        multiMediaMapper.updateMultiMediaWithoutUpdateURL(mediaChangeableInfo);


    }

    @Override
    public Integer getClubIdByMultiMediaId(Integer value6) {
        Integer clubId = multiMediaMapper.selectClubIdByMultiMediaId(value6);
        return clubId;
    }

    @Override
    public MultiMediaFontOfGet getMultiMediaFontOfGetByMultiMediaId(Integer multiMediaId) {
        MultiMediaFontOfGet info = multiMediaMapper.selectMultiMediaFontOfGetByMultiMediaId(multiMediaId);
        return info;
    }

    @Override
    public PageBean getMultiMediaFontOfGetInList(Integer page, Integer pageSize, Integer personId) {

        PageBean pageBean = PageDivideUtil.
                toPageBean(() -> multiMediaMapper.selectMultiMediaFontOfGetInList(personId), page, pageSize);
        return pageBean;
    }

    @Override
    public boolean isAuthor(Integer personId, Integer multiMediaId) {
        Boolean isAuthor = multiMediaMapper.isPersonIdAndMultiMediaIdExist(personId, multiMediaId);
        return isAuthor;
    }

    @Override
    @Transactional
    public void deleteMultiMedia(Integer multiMediaId) {
        // 1.获取记录备份
        MultiMedia media = multiMediaMapper.selectMultiMediaByMultiMediaId(multiMediaId);
        // 2.获取记录相关的url
        String coverUrl = media.getCoverUrl();
        String multiMediaUrl = media.getMultiMediaUrl();
        // 3.删除记录
        multiMediaMapper.deleteByMultiMediaId(multiMediaId);
        // 4.删除相关文件
            uploadService.deleteUrlResourceNotInTable(coverUrl);
            uploadService.deleteUrlResourceNotInTable(multiMediaUrl);

    }

    @Override
    public void changeMultiMediaSuggestStation(Integer isSuggested, Integer multiMediaId) {
        multiMediaMapper.updateSuggestStatusByIsSuggested(isSuggested, multiMediaId);
    }

    @Override
    public PageBean getMultiMediaFontOfGetInStyle(Integer page, Integer pageSize, Integer clubId) {
        PageBean pageBean = PageDivideUtil.toPageBean(() -> multiMediaMapper.selectMultiMediaFontOfGetInStyle(clubId), page, pageSize);
        return pageBean;
    }

    @Override
    public List<MultiMediaFontOfGetInHead> getMultiMediaFontOfGetInHomePage(Integer articleNum, Integer videoNum) {

        List<MultiMediaFontOfGetInHead> lsOfArticle = multiMediaMapper.selectMultiMediaFontOfGetInHeadImage(articleNum, MultiMediaAssist.ARTICLE);
        List<MultiMediaFontOfGetInHead> lsOfVideo = multiMediaMapper.selectMultiMediaFontOfGetInHeadImage(videoNum, MultiMediaAssist.VIDEO);
        List<MultiMediaFontOfGetInHead> list = Stream.concat(lsOfArticle.stream(), lsOfVideo.stream()).toList();

        return list;
    }
}
